package com.jf.cloud.service.sysmanager.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jf.cloud.service.sysmanager.common.base.BaseService;
import com.jf.cloud.service.sysmanager.common.base.GenericsUtils;
import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.constant.TagConstant;
import com.jf.cloud.service.sysmanager.dao.ISysMenuService;
import com.jf.cloud.service.sysmanager.entity.SysMenu;
import com.jf.cloud.service.sysmanager.entity.SysStaff;

/**
 * <p>
 * 菜单 服务实现类
 * </p>
 *
 * @author LangK
 * @since 2017-12-04
 */
@Service
public class SysMenuServiceImpl extends BaseService<SysMenu, Long> implements ISysMenuService {

	@Resource
	private BaseDao baseDao;
	
	private RowMapper<SysMenu> rowMapper = new BeanPropertyRowMapper<SysMenu>(GenericsUtils.getSuperClassGenricType(this.getClass()));
	
	/**
	 * 获取菜单的树
	 */
	public  List<Map<String, Object>> getMenuListByTree() {
		List<SysMenu> findList = this.findMenuList();
		List<Map<String,Object>> menus = new ArrayList<Map<String,Object>>();
		
		for (SysMenu sysMenu : findList) {
			HashMap<String, Object> hashMap = new HashMap<String,Object>();
			Long id = sysMenu.getRowId();
			hashMap.put("id", id);
			hashMap.put("pId", sysMenu.getParentId());
			hashMap.put("fileUrl", "/menu/info?id="+id);
			hashMap.put("name", sysMenu.getMenuName());
			hashMap.put("hasChildren", sysMenu.getParentId() != null && sysMenu.getParentId() != 0);
			hashMap.put("menuType", sysMenu.getMenuFlag());
			menus.add(hashMap);
		}
		return menus;
	}
	
	
	@Override
	public List<SysMenu> findMenuList() {
		String sql = "SELECT * FROM sys_menu WHERE DEL_FLAG = 0 ORDER BY PARENT_ID,ORDER_NUM ";
		return baseDao.findList(sql , rowMapper);
	}
	
	@Override
	public SysMenu findMenuByRightKey(String rightKey) {
		String sql = "SELECT * FROM sys_menu WHERE DEL_FLAG = 0 and MENU_CODE = ? ";
		return baseDao.findObject(sql, rowMapper, rightKey);
	}

	/**
	 * 返回一级菜单列表,供select框用
	 */
	@Override
	public List<Map<String, Object>> getSelectListBy1Level() throws Exception {
		Query query = new Query();
		query.put("PARENT_ID", 0);//一级菜单
		query.put("MENU_FLAG", 1);//一级菜单
		query.put("DEL_FLAG", 0);//未删除
		List<SysMenu> findList = this.findList(query);
		List<Map<String, Object>> selectList = getSelectList(findList);
		return selectList;
	}

	/**
	 * 数据组成下拉框
	 */
	@Override
	public List<Map<String, Object>> getSelectList(List<SysMenu> findList) throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (SysMenu menu : findList) {
			Map<String, Object> option = new HashMap<String, Object>();
			option.put(TagConstant.DICT_OPTION_VALUE_KEY, menu.getRowId());
			option.put(TagConstant.DICT_OPTION_LABEL_KEY, menu.getMenuName());
			list.add(option);
		}
		return list;
	}

	/**
	 * 根据上级菜单ID查询子菜单或子按钮
	 */
	@Override
	public List<SysMenu> getBelongList(Long rowId){
		Query query = new Query();
		query.put("PARENT_ID", rowId);
		query.put("DEL_FLAG", 0);
		List<SysMenu> buttonList = this.findList(query);
		return buttonList;
	}

	/**
	 * 修改菜单
	 */
	@Override
	@Transactional
	public void updateSysmenu(SysMenu sysMenu, SysStaff currUser) throws Exception {
		if (Integer.valueOf(0).equals(sysMenu.getMenuFlag())) {
			//修改按钮
			String sql = "UPDATE sys_menu SET MENU_NAME = ?, ORDER_NUM = ?, URL = ?, ICON = ?, MENU_DESC = ?, "
				+ "OPT_ID = ?, OPT_ACCOUNT = ?, OPT_TIME = NOW() WHERE ROW_ID = ? ";
			this.baseDao.update(sql, sysMenu.getMenuName(), sysMenu.getOrderNum(), sysMenu.getUrl(), sysMenu.getIcon(), sysMenu.getMenuDesc(), currUser.getRowId(), currUser.getStaffAccount(), sysMenu.getRowId());
		} else if (Integer.valueOf(1).equals(sysMenu.getMenuFlag())) {
			//修改一级菜单
			String sql = "UPDATE sys_menu SET MENU_NAME = ?, ORDER_NUM = ?, ICON = ?, MENU_DESC = ?, OPT_ID = ?, "
				+ "OPT_ACCOUNT = ?, OPT_TIME = NOW() WHERE ROW_ID = ?";
			this.baseDao.update(sql, sysMenu.getMenuName(), sysMenu.getOrderNum(), sysMenu.getIcon(), sysMenu.getMenuDesc(), currUser.getRowId(), currUser.getStaffAccount(), sysMenu.getRowId());
		} else {
			//修改二级菜单
			String sql = " UPDATE sys_menu SET PARENT_ID = ?,  MENU_NAME = ?, ORDER_NUM = ?, URL = ?, ICON = ?, MENU_DESC = ?, "
				+ "OPT_ID = ?, OPT_ACCOUNT = ?, OPT_TIME = NOW() WHERE ROW_ID = ?";
			this.baseDao.update(sql, sysMenu.getParentId(), sysMenu.getMenuName(), sysMenu.getOrderNum(), sysMenu.getUrl(), sysMenu.getIcon(), sysMenu.getMenuDesc(), currUser.getRowId(), currUser.getStaffAccount(), sysMenu.getRowId());
		}
	}

	/**
	 * 查询某菜单(按钮添加的时候)
	 */
	@Override
	public SysMenu findSysmenu(Long rowId) throws Exception {
		Query query = new Query();
		query.put("ROW_ID" ,rowId);
		return this.find(query);
	}

	/**
	 * 添加菜单
	 */
	public Boolean saveSysmenu(SysMenu sysMenu, SysStaff currUser) throws Exception {
		sysMenu.setUseFlag(1);
		sysMenu.setDelFlag(0);
		sysMenu.setOpenWay("1");
		sysMenu.setCreateId(currUser.getRowId());
		sysMenu.setCreateAccount(currUser.getStaffAccount());
		sysMenu.setCreateTime(new Timestamp(System.currentTimeMillis()));
		sysMenu.setOptId(currUser.getRowId());
		sysMenu.setOptAccount(currUser.getStaffAccount());
		sysMenu.setOptTime(new Timestamp(System.currentTimeMillis()));
		this.baseDao.saveAll(sysMenu);
		return true;
	}

	/**
	 * 删除菜单
	 */
	@Override
	public Boolean deleteSysmenu(Long rowId, SysStaff currUser) throws Exception {
		String sql = "UPDATE sys_menu SET DEL_FLAG = 1, OPT_ID = ?, OPT_ACCOUNT = ?, OPT_TIME = NOW() WHERE ROW_ID = ?";
		return this.safeUpdate(sql, 1, currUser.getRowId(), currUser.getStaffAccount(), rowId);
	}
}
